package com.longcai.piaojia.conn;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.note.HttpInlet;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/10.
 */
@HttpInlet(Conn.ACTIVITY_TOPIC)
public class LatestDetailTopicGet extends BaseAsyGet<LatestDetailTopicGet.LatestTopicEntity> {

    public String id;
    public int page;

    public LatestDetailTopicGet(AsyCallBack<LatestTopicEntity> asyCallBack) {
        super(asyCallBack);
    }

    @Override
    protected LatestTopicEntity parser(JSONObject object) throws Exception {

        TOAST = object.optString("message");

        if (object.optInt("code") == 200) {

            JSONObject mainObject = object.optJSONObject("data");

            LatestTopicEntity latestTopicEntity = new LatestTopicEntity();

            latestTopicEntity.total = mainObject.optInt("total");
            latestTopicEntity.current_page = mainObject.optInt("current_page");
            latestTopicEntity.per_page = mainObject.optInt("per_page");

            JSONArray topArray = mainObject.optJSONArray("data");

            for (int i = 0; i < topArray.length(); i++) {

                JSONObject evaluteObject = topArray.optJSONObject(i);

                MyEvalute myEvalute = new MyEvalute();

                myEvalute.id = evaluteObject.optString("id");
                myEvalute.p_id = evaluteObject.optString("p_id");
                myEvalute.user_id = evaluteObject.optString("user_id");
                myEvalute.content = evaluteObject.optString("content");
                myEvalute.posttime = evaluteObject.optString("posttime");
                myEvalute.username = evaluteObject.optString("username");

                JSONArray replyArray = evaluteObject.optJSONArray("reply");

                for (int j = 0; j < replyArray.length() ; j++) {

                    JSONObject replyObject = replyArray.optJSONObject(j);

                    LatestReply latestReply = new LatestReply();

                    latestReply.user_id = replyObject.optString("user_id");
                    latestReply.content = replyObject.optString("content");
                    latestReply.username = replyObject.optString("username");
                    latestReply.posttime = replyObject.optString("posttime");

                    myEvalute.replyList.add(latestReply);

                }

                latestTopicEntity.myEvaluteList.add(myEvalute);
            }

            return latestTopicEntity;
        }

        return null;
    }

    public static class LatestTopicEntity implements Serializable {

        public int total, per_page, current_page;

        public List<MyEvalute> myEvaluteList = new ArrayList<>();

    }

    public static class MyEvalute implements Serializable {

        public String id, p_id, user_id, content, posttime, username;

        public List<LatestReply> replyList = new ArrayList<>();
    }

    public static class LatestReply implements Serializable {

        public String user_id, content, username, posttime;

    }

    /*
    * {
    "code": 200,
    "data": {
        "total": 2,           //总条数
        "per_page": "10", //每页多少条
        "current_page": 1, //当前页
        "data": [
            {
                "id": 4,
                "p_id": 1,
                "user_id": 1,
                "content": "测试评论、测试评论", //评论
                "posttime": "27分钟前",  // 时间
                "username": "阿坤",  //昵称
                "reply": []
            },
            {
                "id": 1,
                "p_id": 1,
                "user_id": 2,
                "content": "测试评论、测试评论", //评论
                "posttime": "27分钟前",  // 时间
                "username": "阿坤",  //昵称
                "reply": [   //回复
                    {
                        "user_id": 3,
                        "content": "回复 回复 回单付",  //内容
                        "username": "阿坤",  //昵称
                        "posttime": "29分钟前" //时间
                    },
                    {
                        "user_id": 4,
                        "content": "啊实打实大电饭锅电饭锅电饭锅电饭锅",
                        "username": "阿坤",  //昵称
                        "posttime": "27分钟前"
                    }
                ]
            }
        ]
    }
}*/

}
